﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class BancosUC
    Dim objBLL As New BancosBLL
    
#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.localizar() '*aqui
        DGVDados.Refresh()
    End Sub
#End Region
#Region "KeyPress"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
    End Sub
#End Region
#Region "Load"
    Private Sub BancosUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
    End Sub
#End Region
#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TSBLocalizar.CheckState = 0
            TXTLocalizar.Text = ""
        Else
            PLocalizar.Visible = True
            TSBLocalizar.CheckState = 1
            TXTLocalizar.Text = ""
        End If
    End Sub
#End Region
#Region "TextBox Localizar"
    Private Sub TXTLocalizar_TextChanged_1(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Dim texto As String = Nothing
        'Primeiro Percorre todo o datagrid
        For Each linha As DataGridViewRow In DGVDados.Rows
            'depois percorro todas as colunas da linha
            For Each celula As DataGridViewCell In DGVDados.Rows(linha.Index).Cells
                'seleciono qual coluna eu vou comparar
                If Trim(TXTLocalizar.Text) <> "" Then
                    If celula.ColumnIndex = CBFiltro.SelectedIndex Then
                        'Gravo o Valor que existe Naquela Coluna
                        texto = celula.Value.ToString.ToLower
                        'Se o que esta na coluna contem algo com o que eu escrevi no TextBox
                        If texto.Contains(TXTLocalizar.Text.ToLower) Then
                            'Entao seleciona a linha para mim
                            Me.DGVDados.Rows(celula.RowIndex).Visible = True

                        Else
                            'Else Desmarca a linha
                            DGVDados.CurrentCell = Nothing
                            Me.DGVDados.Rows(celula.RowIndex).Visible = False
                        End If
                    End If
                Else
                    'Else campo estiver Vazio
                    'posiciona o scroll na posição que inicial ou seja na linha 0 
                    'Me.dgSolicitante.FirstDisplayedScrollingRowIndex = 0
                    Me.DGVDados.Rows(celula.RowIndex).Visible = True
                End If
            Next
        Next
    End Sub
#End Region
#Region "Novo"
    Private Sub TSBNovo_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        CadastrodeBancosFRM.Op = "I"
        TSBNovo.BackColor = Color.Gainsboro
        CadastrodeBancosFRM.ShowDialog()
        TSBNovo.BackColor = Color.Transparent
        CarregaGrid()
    End Sub
#End Region
#Region "Alterar"
    Private Sub TSBAlterar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ban_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                CadastrodeBancosFRM.Codigo = Val(DGVDados.Rows(indice).Cells("ban_codigo").Value)
                CadastrodeBancosFRM.Op = "A"
                TSBAlterar.BackColor = Color.Gainsboro
                CadastrodeBancosFRM.ShowDialog()
                CarregaGrid()
            Else
                MessageBox.Show("Selecione o Banco a ser alterado", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Excluir"
    Private Sub TSBExcluir_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ban_codigo") Is Nothing) Then
                    objBLL = New BancosBLL(Val(DGVDados.Rows(linha.Index).Cells("ban_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.pbanCodigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If (MessageBox.Show("Confirma a exclusão do banco: " & objBLL.pbanCodigo & "?", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                    objBLL.pstatus = 3
                    If (objBLL.Alterar()) Then
                        MsgBox("Exclusão efetuada com sucesso!", MsgBoxStyle.Information)
                        CarregaGrid()
                    Else
                        MsgBox("Banco não pode ser excluído", MsgBoxStyle.Exclamation)
                    End If
                End If
            End If
            TSBExcluir.BackColor = Color.Transparent
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region
#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub
#End Region

    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 3 Then
            TXTLocalizar.Visible = False
        Else
            TXTLocalizar.Visible = True
            TXTLocalizar.Focus()
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 1 Then
            TXTLocalizar.Visible = True
        Else
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
    End Sub

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
    Private Sub TSBRelatorio_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("ban_codigo") Is Nothing) Then
                objBLL = New BancosBLL(Val(DGVDados.Rows(linha.Index).Cells("ban_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.pbanCodigo > 0) Then gerarelatrio(objBLL.pbanNome, "{Gen_Bancos.ban_codigo}=" & objBLL.pbanCodigo)
        End If
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosfinFRM
            Dim rpt As New BancosDetalhado

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer2.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer2.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
End Class
